package com.qf.dao.impl;

import com.qf.dao.LogDao;
import com.qf.model.Log;
import com.qf.model.PageBean;
import com.qf.model.pojo.Userloans;
import com.qf.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class LogDaoImpl extends BaseDao implements LogDao {
    private QueryRunner queryRunner=new QueryRunner(DruidUtil.getDataSource());
    //添加日志 事务
    @Override
    public int addLog(Connection conn, Log log) {
        try {
            String sql="insert into log values(null,?,?,?,?,?,?,?,?,null,null,null,null,null)";
            int num=queryRunner.update(conn,sql,
                    log.getIdcade(),
                    log.getPhone(),
                    log.getC_id(),
                    log.getMoney(),
                    log.getMoneytype(),
                    log.getDate(),
                    log.getType(),
                    log.getMessage()
            );
            return num;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
    //查询日志

    @Override
    public List<Log> findAll(PageBean<Log> pageBean, String idcade,int type) {
        //0普通用户  1：管理员
        if(type==0){
            String sql1="select * from log  where idcade=? order by id desc ";
            List<Log> list = queryForList(Log.class,sql1,idcade);
            pageBean.setCount(list.size());
            String sql="select * from log  where idcade=?  order by id desc limit ?,?";
            int i=(pageBean.getCurPage()-1)*pageBean.getPageSize();
            List<Log> list1 = queryForList(Log.class, sql,idcade, i, pageBean.getPageSize());
            return list1;
        }else {
            String sql1="select * from log order by id desc";
            List<Log> list = queryForList(Log.class,sql1);
            pageBean.setCount(list.size());
            String sql="select * from log order by id desc limit ?,?";
            int i=(pageBean.getCurPage()-1)*pageBean.getPageSize();
            List<Log> list1 = queryForList(Log.class, sql, i, pageBean.getPageSize());
            return list1;
        }

    }
}
